﻿using System;

namespace DotNetCommon.Validate.Annotations;

/// <summary>
/// 字符串必须以给定的值结尾, 示例:
/// <code>
/// [VMustEnds("大会")]
/// public string Title { get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustEndsAttribute(string value, bool ignoreCase = false, string errorMessage = null) : Attribute, IValidateAction
{
    public string Value { get; set; } = value;
    public bool IgnoreCase { get; } = ignoreCase;
    public string ErrorMessage { get; set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        ValidateStringExtensions.MustEndsWithInternal(ctx, ctx.ModelValue?.ToString(), Value, IgnoreCase, ErrorMessage);
    }
}

/// <summary>
/// 如果不为null, 则字符串必须以给定的值结尾, 示例:
/// <code>
/// [VMustEndsIfNotNull("大会")]
/// public string Title { get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustEndsIfNotNullAttribute(string value, bool ignoreCase = false, string errorMessage = null) : Attribute, IValidateAction
{
    public string Value { get; set; } = value;
    public bool IgnoreCase { get; } = ignoreCase;
    public string ErrorMessage { get; set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        if (ctx.ModelValue == null) return;
        ValidateStringExtensions.MustEndsWithInternal(ctx, ctx.ModelValue?.ToString(), Value, IgnoreCase, ErrorMessage);
    }
}
